﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace buildem
{
    class BuildFiles
    {
        public static string[] ParseList(string[] files)
        {
            List<string> _list = new List<string>();
            foreach (var f in files)
            {
                if (f.IndexOf("./out") == 0) continue;
                if (f.IndexOf("./temp") == 0) continue;
                if (f.IndexOf("./.vscode") == 0) continue;
                _list.Add(f);
            }
            return _list.ToArray();

        }
        public string[] hfiles;
        public string[] hppfiles;
        public string[] cfiles;
        public string[] cppfiles;
        public string[] jsfiles;

        public string GenBuildBat()
        {

            StringBuilder outstr = new StringBuilder();
            outstr.AppendLine("@ECHO EMSDK Builder");

            outstr.AppendLine("@ECHO InitEMSDK");
            outstr.AppendLine("call c:\\git\\emsdk\\emsdk_env");

            foreach (var f in hfiles)
            {
                outstr.AppendLine("@ECHO Heaser='" + f + "'");
            }
            foreach (var f in hppfiles)
            {
                outstr.AppendLine("@ECHO Heaser='" + f + "'");
            }
            foreach (var f in cfiles)
            {
                outstr.AppendLine("@ECHO Code='" + f + "'");
            }
            foreach (var f in cppfiles)
            {
                outstr.AppendLine("@ECHO Code='" + f + "'");
            }
            foreach (var f in jsfiles)
            {

                outstr.AppendLine("@ECHO Code(js)='" + f + "'");
            }
            outstr.AppendLine("@ECHO Build c/c++");
            //add build part
            foreach (var f in cfiles)
            {
                var ofile = System.IO.Path.GetFileName(f) + ".o";
                outstr.AppendLine("call emcc -gsource-map " + f + " -c -o ./tmp/" + ofile);
            }
            foreach (var f in cppfiles)
            {
                var ofile = System.IO.Path.GetFileName(f) + ".o";
                outstr.AppendLine("call emcc -gsource-map " + f + " -c -o ./tmp/" + ofile);
            }
            //add link part
            string linkstr = "call emcc ";
            foreach (var f in cfiles)
            {
                var ofile = System.IO.Path.GetFileName(f) + ".o";
                linkstr += " ./tmp/" + ofile + " ";
            }
            foreach (var f in cppfiles)
            {
                var ofile = System.IO.Path.GetFileName(f) + ".o";
                linkstr += " ./tmp/" + ofile + " ";
            }
            foreach (var f in jsfiles)
            {
                linkstr += "--js-library " + f + " ";
            }
            linkstr += "-lGL -sUSE_WEBGL2=1 -sTEXTDECODER=2 -sSUPPORT_ERRNO=0 -sFILESYSTEM=0 -sMINIMAL_RUNTIME=2 -sENVIRONMENT=web -sLLD_REPORT_UNDEFINED -sALLOW_MEMORY_GROWTH";
            var linkrelease = linkstr + " -o ./out/t_release.html -O1";
            var linkdebug = linkstr + " -o ./out/t_debug.html -gsource-map --source-map-base http://127.0.0.1:5500/out/";
            outstr.AppendLine("@ECHO Link Release");
            outstr.AppendLine(linkrelease);
            outstr.AppendLine("@ECHO Link Debug");
            outstr.AppendLine(linkdebug);
            return outstr.ToString();
        }

        public void Run(string bat)
        {
            System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
            info.FileName = System.IO.Path.GetFileName(bat);
            info.WorkingDirectory = System.IO.Path.GetDirectoryName(System.IO.Path.GetFullPath(bat));
            var p = System.Diagnostics.Process.Start(info);
            p.WaitForExit();
        }
    }
}
